<?php

namespace App\Http\Controllers\VideoAdmin;

use App\Http\Config\ErrorCode;
use App\Http\Controllers\Admin\BaseController;
use App\Models\video\NavigationModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class NavigationController extends BaseController
{
    private $field = [
        "name" => "require", "icon_id" => "require", "path" => 'require', 'sort' => 0, 'status' => 1
    ]; //填入数据库需要存储的表字段
    private $fieldMsg = ["name" => "标题", "icon_id" => "图标", "path" => "页面路径"];
    private $data = [];

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    public function getList(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->return_result($this->returnData);
        }
        $page = $request->page ?: 1;
        $pageSize = $request->pageSize ?: 10;
        $data = NavigationModel::getList("*", ["mall_id" => $request->mallData["id"]], $page, $pageSize);
        foreach ($data["list"] as &$item){
            $item["pathName"] = DB::table("up_wx_path")->where("url", $item["path"])->value("title");
        }
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    public function getAll(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $data = NavigationModel::getFields("name,path", ["mall_id" => $request->mallData["id"]], false, false);
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    // 更新状态
    public function updateStatus(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        if(NavigationModel::doUpdate(["status" => $request->status ?: 0], ["id" => $request->id])){
            if($request->status == '1' && ($count = NavigationModel::getCount(["status" => 1, "mall_id" => $request->mallData["id"]])) >= 5){
                NavigationModel::query()->where("mall_id", $request->mallData["id"])->orderBy("sort", "asc")->limit($count-5+1)->update(["status" => 0]);
            }
        }
        return $this->return_result($this->returnData);
    }

    // 添加
    public function create(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        foreach ($this->field as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !$request->$ks){
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            $this->data[$k] =  $request->$ks ? trim($request->$ks) : $v;
        }
        if($this->data["status"] == '1' && ($count = NavigationModel::getCount(["status" => 1, "mall_id" => $request->mallData["id"]])) >= 5){
            NavigationModel::query()->where("mall_id", $request->mallData["id"])->orderBy("sort", "asc")->limit($count-5+1)->update(["status" => 0]);
        }
        NavigationModel::toAdd($this->data);
        return $this->return_result($this->returnData);
    }

    // 更新
    public function updateInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        foreach ($this->field as $k => $v) {
            $ks = Str::camel($k); //转换字段，变驼峰
            if($v === 'require' && !$request->$ks){
                return ErrorCode::code("params_not_exist", $this->fieldMsg[$k]);
            }
            if($request->$ks) {
                $this->data[$k] = trim($request->$ks);
            }
        }
        if(!empty($this->data["status"]) && $this->data["status"] == '1' && ($count = NavigationModel::getCount(["status" => 1, "mall_id" => $request->mallData["id"]])) >= 5){
            NavigationModel::query()->where("mall_id", $request->mallData["id"])->orderBy("sort", "asc")->limit($count-5+1)->update(["status" => 0]);
        }
        NavigationModel::doUpdate($this->data, ["id" => $request->id]);
        return $this->return_result($this->returnData);
    }

    // 获取信息
    public function getInfo(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $data = NavigationModel::getFields("*", ["id" => $request->id]);
        if(!empty($data)){
            $this->returnData["data"] = $data;
        }else{
            $this->returnData["data"] = [];
        }
        return $this->return_result($this->returnData);
    }

    // 删除
    public function delete(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        NavigationModel::delById($request->id);
        return $this->returnData;
    }
}
